/**
 * getCanvasText 画布文字超出省略
 * text 文本
 * ctx 主体
 * XStart 起始X坐标
 * YStart 起始Y坐标
 * width 一行宽度
 * */
export function getCanvasText(text, ctx, XStart, YStart, width, rowNum) {
	var chr = text.split("");
	var temp = "";
	var row = [];
	for (var a = 0; a < chr.length; a++) {
		if (ctx.measureText(temp).width < width) {
			temp += chr[a];
		} else {
			a--; //这里添加了a-- 是为了防止字符丢失，效果图中有对比
			row.push(temp);
			temp = "";
		}
	}
	row.push(temp);
	if (row.length > rowNum) {
		var rowCut = row.slice(0, rowNum);
		var rowPart = rowCut[rowNum - 1];
		var test = "";
		var empty = [];
		for (var a = 0; a < rowPart.length; a++) {
			if (ctx.measureText(test).width < (width - 10)) {
				test += rowPart[a];
			} else {
				break;
			}
		}
		empty.push(test);
		var group = empty[0] + "..." //这里只显示三行，超出的用...表示
		rowCut.splice(rowNum - 1, 1, group);
		row = rowCut;
	}
	for (var b = 0; b < row.length; b++) {
		ctx.fillText(row[b], XStart, YStart + b * 18);
	}
}
/**
 * roundRect 画布圆角
 *
 *
 *
 *
 *
 * */
export function roundRect(ctx, x, y, width, height, radius) {
	ctx.save();
	ctx.beginPath();
	// 左上角
	ctx.moveTo(x, y);
	ctx.arc(x + radius, y + radius, radius, Math.PI, Math.PI * 1.5);
	// 右上角
	ctx.lineTo(x + width - radius, y);
	ctx.arc(x + width - radius, y + radius, radius, Math.PI * 1.5, Math.PI * 2);
	// 右下角
	ctx.lineTo(x + width, y + height - radius);
	ctx.arc(x + width - radius, y + height - radius, radius, 0, Math.PI * 0.5);
	// 左下角
	ctx.lineTo(x + radius, y + height);
	ctx.arc(x + radius, y + height - radius, radius, Math.PI * 0.5, Math.PI);
	ctx.closePath();
	// 填充
	ctx.fill();
	// 剪切
	ctx.clip()

}

// 画布文字超出省略号
// export function getTextEllipsis(ctx, text, widthMin) {
//   if (ctx.measureText(text).width > _self.offsetWidth - 24 - widthMin) {
//     var nameTest = '';
//     var newName = [];
//     for (var a = 0; a < text.length; a++) {
//       if (ctx.measureText(nameTest).width < _self.offsetWidth - 24 - widthMin) {
//         nameTest += text[a];
//       } else {
//         break;
//       }
//     }
//     return nameTest + "...";
//   } else {
//     return text;
//   }
// }

// let images = [];
// let imgIndex = 0;
export function getImageInfo(imgList, images, imgIndex, callback) {
	let resImageList = [];
	let newTmpHeaderImg = wx.getStorageSync('newTmpHeaderImg');
	imgList.map((item, index) => {
		var imageLinkPath = item.img.replace('http://', 'https://')
		imageLinkPath = item.img.replace('https://thirdwx.qlogo.cn', 'https://wx.qlogo.cn')
		// imageLinkPath = item.img.replace('.primary.png', '')

		uni.getImageInfo({
			src: imageLinkPath,
			success: res => {
				if (imgList[index].type == 'none' && newTmpHeaderImg) {
					images[index] = newTmpHeaderImg;
				} else {
					images[index] = res.path;
				}
				// item.value = res.path
				resImageList.push(item)
				if (resImageList.length == imgList.length) {
					callback(images)
				}
			},
			fail: function(err) {
				console.log(err)
			}
		})
	})
}

export function getImageHeightInfo(imgList, images, imgIndex, screenWidth, callback) {

	if (imgList && imgList.length > 0) {
		let imgUrl = imgList[imgIndex].replace('http://', 'https://');
		let imgItem = {
			imgUrl: imgUrl,
			width: screenWidth,
			height: 0
		}
		wx.getImageInfo({
			src: imgUrl,
			success: function(res) {
				imgItem.height = screenWidth / (res.width / res.height)
				images.push(imgItem)
				if (images.length >= imgList.length) {
					callback(images)
				} else {
					imgIndex++;
					getImageHeightInfo(imgList, images, imgIndex, screenWidth, callback)
				}
			},
			fail: function(err) {
				uni.hideLoading();
				wx.showToast({
					title: `网络错误请重试${imgIndex}`,
					icon: 'loading',

				});
			}
		})
	}
}